﻿using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Primitives;
using Newtonsoft.Json;
using ST.Common.Util.Model;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;

namespace ST.Common.Util.Mvc
{
	public class BaseViewComponent : ViewComponent
	{
		protected readonly ILogger logger;

		protected BaseViewComponent()
		{
		}

		protected BaseViewComponent(ILogger<BaseController> _logger) : this()
		{
			logger = _logger;
		}

		protected BaseViewComponent(ILogger _logger) : this()
		{
			logger = _logger;
		}

		public virtual IViewComponentResult Invoke()
		{
			return View();
		}

		protected virtual UserSession GetSessionFromRequest()
		{
			try
			{
				return HttpContextHelper.GetSessionFromRequest(Request, false);
			}
			catch (AuthException ex)
			{
				logger.LogError(ex.InnerException ?? ex, ex.Message);
				throw ex;
			}
		}
	}
}
